package com.dh.project.controller;

import com.alibaba.fastjson.JSON;
import com.dh.project.model.NewManage;
import com.dh.project.model.push.PageType;
import com.dh.project.other.ModelRes;
import com.dh.project.service.NewManageService;
import com.dh.project.utils.PushUtil;
import com.dh.project.utils.StringConfigUtil;
import com.dh.project.utils.StringUtil;
import com.dh.project.utils.WorkerPushUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.*;

/**
 * Created by admin on 2017/5/17.
 */
@Controller
public class NewsController {

    private NewManageService newManageService;

    @Autowired
    public NewsController(NewManageService newManageService){
        this.newManageService = newManageService;
    }

    /**
     * 查询新闻活动
     * @return
     */
    @RequestMapping(value = "selectNewsList",produces = "text/html;charset=utf-8")
    public String selectNewsList(Model model){
        Map map = new HashMap();
        List<NewManage> newManageList = newManageService.selectListByMap(map);
        model.addAttribute("newManageList",newManageList);
        return "news_list";
    }

    /**
     * 上传新闻图片
     * @param file
     * @param suf
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "uploadingNewsImg", produces = "text/html;charset=utf-8")
    public Object uploadingNewsImg(String file,String suf,String title,String news_content,String text) {

        String fileName = "news_"+System.currentTimeMillis()+suf;

        boolean isOk = StringUtil.generateImage(file, StringConfigUtil.ImgAddress+fileName);
        if (isOk){

            Map map = new HashMap();
            map.put("new_number",1);
            List<NewManage> newManages = newManageService.selectByMap(map);
            if (newManages.size() != 0){

                Map allMap = new HashMap();
                List<NewManage> allNewManages = newManageService.selectByMap(allMap);
                int two = 0,three=0,four=0;
                for (int i = 0; i < allNewManages.size(); i++) {
                    if (allNewManages.get(i).getNew_number() == 2){
                        two = 1;
                    }
                    if (allNewManages.get(i).getNew_number() == 3){
                        three = 1;
                    }
                    if (allNewManages.get(i).getNew_number() == 4){
                        four = 1;
                    }
                }
                if (two == 0){
                    newManages.get(0).setNew_number(2);
                }else if (three == 0){
                    newManages.get(0).setNew_number(3);
                }else if (four == 0){
                    newManages.get(0).setNew_number(4);
                }
                newManageService.updateById(newManages.get(0));
            }
            NewManage newManage = new NewManage();
            newManage.setNew_number(1);
            newManage.setTitle(title);
            newManage.setContent(news_content);
            newManage.setImg_name(fileName);
            newManage.setCreatetime(String.valueOf(new Date().getTime()));
            newManage.setUpdateTime(new Date());
            newManage.setText(text);
            newManageService.insert(newManage);
            return new ModelRes(ModelRes.Status.SUCCESS,fileName);
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"上传失败");
        }
    }

    /**
     * 修改图片
     * @param file
     * @param suf
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateUploadingNewsImg", produces = "text/html;charset=utf-8")
    public Object updateUploadingNewsImg(String file,String suf,int id) {
        NewManage newManage = newManageService.selectById(id);

        String path = StringConfigUtil.ImgAddress;
        File file1 = new File(path+newManage.getImg_name());
        if (file1.exists()){
            file1.delete();
        }
        String fileName = "news_"+System.currentTimeMillis()+suf;


        boolean isOk = StringUtil.generateImage(file,path+fileName);
        if (isOk){
            newManage.setImg_name(fileName);
            newManageService.updateById(newManage);
            return new ModelRes(ModelRes.Status.SUCCESS,fileName);
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改图片失败");
        }
    }

    /**
     * 修改新闻内容
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateNews", produces = "text/html;charset=utf-8")
    public Object deleteNews(NewManage newManage) {
        NewManage newManage1 = newManageService.selectById(newManage.getId());

        if (newManage.getTitle() != null){
            newManage1.setTitle(newManage.getTitle());
        }

        if (newManage.getContent() != null){
            newManage1.setContent(newManage.getContent());
        }
        if(newManage.getText() != null){
            newManage1.setText(newManage.getText());
        }
        newManage1.setUpdateTime(new Date());
        boolean isOk = newManageService.updateById(newManage1);
        //服务器地址
//        boolean isOk = StringUtil.generateImage(file,"/opt/local/img/"+fileName);
        if (isOk){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败！");
        }
    }

    /**
     * 删除活动
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteNews", produces = "text/html;charset=utf-8")
    public Object deleteNews(int admin_id) {
        NewManage newManage = newManageService.selectById(admin_id);
        String path = StringConfigUtil.ImgAddress;
        File file = new File(path+newManage.getImg_name());
        if (file.exists()){
            file.delete();
        }
        boolean isOk = newManageService.deleteById(admin_id);
        //服务器地址
        if (isOk){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败");
        }
    }

    /**
     * 查询新闻活动详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "selectNewsManageDetail", produces = "text/html;charset=utf-8")
    public Object selectNewsManageDetail(int id,Model model) {
        NewManage newManage = newManageService.selectById(id);
        model.addAttribute("newManage",newManage);
        return "news_detail";
    }

    /**
     * 推送
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "tuiSong", produces = "application/json;charset=utf-8")
    public Object tuiSong(int id) {
        NewManage newManage = newManageService.selectById(id);
        if(newManage.getText()==null||newManage.getContent()==null||newManage.getContent().equals("")||newManage.getText().equals("")){
            return new ModelRes(ModelRes.Status.ERROR,"推送内容不完整!");
        }
        List list = new ArrayList();
        list.add("client");
        PageType pageType = new PageType(1,newManage.getContent());
        PushUtil.pushTag(list,newManage.getText(),JSON.toJSON(pageType));
       // WorkerPushUtil.pushTag(list,newManage.getText(),JSON.toJSON(pageType));
        return new ModelRes(ModelRes.Status.SUCCESS);
    }


    /**
     * 修改新闻活动顺序
     * @param new_id
     * @param number
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateNewNumber", produces = "text/html;charset=utf-8")
    public Object updateNewNumber(int new_id,int number) {
        NewManage newManage = newManageService.selectById(new_id);
        int old_number = newManage.getNew_number();
        try {
            Map map = new HashMap();
            map.put("new_number",number);
            List<NewManage> newManages = newManageService.selectByMap(map);
            if (newManages.size() != 0){
                newManages.get(0).setNew_number(old_number);
                newManageService.updateById(newManages.get(0));
            }
            newManage.setNew_number(number);
            newManageService.updateById(newManage);
        }catch (Exception e){
            e.printStackTrace();
            return new ModelRes(ModelRes.Status.ERROR,"程序出错");
        }
        return new ModelRes();
    }

}
